package com.cong.nio.echo;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class NioClient {
    public static void main(String[] args) throws IOException {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 120, 0, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(10));
        for (int i = 0; i < 100; i++) {
            executor.execute(NioClient::doConnect);
        }
        executor.shutdown();
    }
   private static void doConnect()  {
       try {
           Socket socket = new Socket("127.0.0.1",8888);
           OutputStream outputStream = socket.getOutputStream();
           String s = "hello world"+Thread.currentThread().getName();
           outputStream.write(s.getBytes(StandardCharsets.UTF_8));
           outputStream.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}
